﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace elements
{
    internal class Program
    {
        static void Main(string[] args)
        {//求用户指定数据的所有素数因子：
         //用最小的素数依次分解，先从2开始，以次找到相应的素数

            Console.WriteLine("请输入要分解的数：");
            string stringNumber = Console.ReadLine();

            if (!int.TryParse(stringNumber, out int num)||num<2)
            {
                Console.WriteLine("请输入一个大于等于2的整数！");
                return;
            }

            List<int> primeFactors = GetPrimeFactors(num);
            Console.WriteLine($"数字{num}的素数因子有：");

            foreach (int factor in primeFactors)
            {
                Console.WriteLine(factor);
            }

            //用于方面检查，读取任意字符后窗台关闭
            Console.ReadLine();
            
            //分解素数因子
            List<int> GetPrimeFactors (int number)
            {
                List<int> factors = new List<int>();
                for (int div = 2; div * div <= number; div++)
                {
                    while (number % div == 0)
                    {
                        factors.Add(div);
                        number /= div;
                    }
                }
                if (number > 1)
                {
                    factors.Add(number);
                }
                return factors;

            }
        }
    }
}
